package org.weasel.views;

import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.List;
import org.weasel.business.Developer;
import org.weasel.controllers.DeveloperCompositeController;

public class DeveloperComposite extends Composite {

	private DeveloperCompositeController controller;
	private List listSpecialDev;
	private Button buttonAddProjDev;
	private Group groupDevelopers;
	private Composite compositeDevButtons;
	private Button buttonRemoveProjDev;
	private Group groupProjDev;
	private List listGeneralDev;

	/**
	 * 
	 * @param parent
	 * @param style
	 * @param generalDevelopers = always the whole (companyDevelopers if projectDetail; projectDevelopers if userSToryDetail)
	 * @param specialDev = the special developers to be added to project or userstory
	 * @param controller TODO
	 */
	public DeveloperComposite(Composite parent, int style, ArrayList<Developer> generalDevelopers, ArrayList<Developer> specialDev, DeveloperCompositeController controller) {
		super(parent, style);
		initialize();
		this.controller = controller;
		loadDeveloperLists(generalDevelopers, specialDev);
	}
	
	/**
	 * Is called to initially load fill the lists and also from outside by the controller to
	 * update the composite if movements are done
	 * @param generalList
	 * @param specialList
	 */
	public void loadDeveloperLists(ArrayList<Developer> generalList, ArrayList<Developer> specialList){
		listGeneralDev.removeAll();
		listSpecialDev.removeAll();
//		show company developers without those selected for the current project
		for (Developer developer : generalList) {
			if(!containsDeveloper(specialList, developer)){
				//if it is not present in the project-dev-list
				listGeneralDev.add(developer.getName() + " (" + developer.getId() + ")");
			}
		}
		
		for (Developer developer : specialList) {
			listSpecialDev.add(developer.getName() + " (" + developer.getId() + ")");
		}
	}

	/**
	 * Needed to exclude those developers from the general Developer list(left) which are already involved in the
	 * current project/userstory (right list)
	 * @param list - list to check
	 * @param dev - developer object for which to check
	 * @return true if it is present in List "list"; otherwise false
	 */
	private boolean containsDeveloper(ArrayList<Developer> list, Developer dev){
		for (Developer developer : list) {
			if(developer.getId().equals(dev.getId()))
				return true;
		}
		return false;
	}

	private void initialize() {
		setSize(new Point(574, 368));
		setLayout(new FillLayout());
		createGroupDevelopers();
	}
	
	private void createGroupDevelopers() {
		groupDevelopers = new Group(this, SWT.NONE);
		groupDevelopers.setLayout(new FillLayout());
		groupDevelopers.setText("Developers");
		createGroupAllDev();
		createCompositeDevButtons();
		createGroupProjDev();
	}

	/**
	 * This method initializes groupAllDev	
	 *
	 */
	private void createGroupAllDev() {
		Group groupAllDev = new Group(groupDevelopers, SWT.NONE);
		groupAllDev.setLayout(new FillLayout());
		groupAllDev.setText("available developers");
		listGeneralDev = new List(groupAllDev, SWT.MULTI);
	}

	/**
	 * This method initializes groupProjDev	
	 *
	 */
	private void createGroupProjDev() {
		groupProjDev = new Group(groupDevelopers, SWT.NONE);
		groupProjDev.setLayout(new FillLayout());
		groupProjDev.setText("participating developers");
		listSpecialDev = new List(groupProjDev, SWT.MULTI);
	}

	/**
	 * This method initializes compositeDevButtons	
	 *
	 */
	private void createCompositeDevButtons() {
		compositeDevButtons = new Composite(groupDevelopers, SWT.NONE);
		compositeDevButtons.setLayout(null);
		buttonRemoveProjDev = new Button(compositeDevButtons, SWT.NONE);
		buttonRemoveProjDev.setBounds(new Rectangle(45, 60, 50, 29));
		buttonRemoveProjDev.setImage(new Image(Display.getCurrent(), getClass().getResourceAsStream("/icons/left_arrow_nav.gif")));
		buttonRemoveProjDev.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
					controller.removeSpecialDeveloper();
			}
		});
		buttonAddProjDev = new Button(compositeDevButtons, SWT.NONE);
		buttonAddProjDev.setBounds(new Rectangle(120, 60, 50, 29));
		buttonAddProjDev.setImage(new Image(Display.getCurrent(), getClass().getResourceAsStream("/icons/right_arrow_nav.gif")));
		buttonAddProjDev.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
				controller.addSpecialDeveloper();
			}
		});
	}
	
	public List getListSpecialDev(){
		return listSpecialDev;
	}
	
	public List getListGeneralDev(){
		return listGeneralDev;
	}
	
	public String[] getSelectedSpecialDevelopers(){
		return listSpecialDev.getSelection();
	}
	
	public String[] getSelectedGeneralDevelopers(){
		return listGeneralDev.getSelection();
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
